using System;
using System.Linq;
using Generator.Model;

namespace Generator.Renderer.Public;

internal static class AliasPrimitiveValueType
{
    public static string Render(GirModel.Alias alias)
    {
        if (alias.Type is not GirModel.PrimitiveValueType)
            throw new Exception($"Can't render alias {alias.Name}. Must be a primitive value type");

        var type = Model.Type.GetName(alias.Type);

        return $@"
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

#nullable enable

namespace {Namespace.GetPublicName(alias.Namespace)};

// AUTOGENERATED FILE - DO NOT MODIFY

{PlatformSupportAttribute.Render(alias as GirModel.PlatformDependent)}
[StructLayout(LayoutKind.Explicit)]
public readonly partial record struct {alias.Name} 
{{
    [FieldOffset(0)]
    private readonly {type} _value;

    public {alias.Name}({type} value)
    {{
        _value = value;
    }}

    public static implicit operator {type}({alias.Name} o) => o._value;
    public static implicit operator {alias.Name}({type} o) => new {alias.Name}(o);
}}";
    }
}
